package com.opgea.crm.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.TemporalType;

import org.springframework.stereotype.Repository;

import com.opgea.crm.common.constant.ApplicationConstant;
import com.opgea.crm.common.util.DateUtil;
import com.opgea.crm.domain.entities.Opportunity;

@Repository
public class OpportunityRepoImpl implements OpportunityRepo {

	@PersistenceContext
	private EntityManager em;
	
	
	public void create(Opportunity opportunity) {
		em.persist(opportunity);
	}
	
	public void update(Opportunity opportunity)
	{
		try{
			em.merge(opportunity);
			
		}
		catch(Exception e){
			e.printStackTrace();
		}
	
	}
	
	public void remove(long id) {
		// TODO Auto-generated method stub
		Opportunity opportunity = em.find(Opportunity.class,id);
		em.remove(opportunity);
	}

	@Override
	public Opportunity find(long id) {
		return em.find(Opportunity.class, id);
	}

	
	public List<Opportunity> findAll() {
		String query = "SELECT o FROM Opportunity o";
		List<Opportunity> opportunityList = em.createQuery(query, Opportunity.class).getResultList();
		return opportunityList;
	}
	
	
	
	@Override
	public List<Opportunity> findOpportunityList(String criteria, String value) {
		
		StringBuffer buffer=new StringBuffer("SELECT o FROM Opportunity o");
		String cond=" WHERE ";
		
		System.out.println("Criteria : "+criteria);
		
		if( (Integer.valueOf(criteria) == ApplicationConstant.OpportunitySearchCriteriaQualifier.OpportunityTitle.ordinal())
				){
			buffer.append(cond);
			buffer.append(" o.opportunityTitle like :paramVal");
			
			}
		
		if((Integer.valueOf(criteria) == ApplicationConstant.OpportunitySearchCriteriaQualifier.CompanyName.ordinal())
				){
			buffer.append(cond);
			buffer.append(" o.relatedContact.company.name like :paramVal");
			
		}
		
		if((Integer.valueOf(criteria) == ApplicationConstant.OpportunitySearchCriteriaQualifier.SalesPerson.ordinal())
		){
			buffer.append(cond);
			buffer.append(" o.salesPerson.firstName like :paramVal");
			
		}
		
		Query query=em.createQuery(buffer.toString());
		query.setParameter("paramVal", value+'%');
		
		return (List<Opportunity>)query.getResultList();
	}	


	@Override
	public List<Opportunity> findAllByLeadId(long leadId) {
		String query = "SELECT o FROM Opportunity o WHERE o.relatedContact.id = "+leadId;
		List<Opportunity> opportunityList = em.createQuery(query, Opportunity.class).getResultList();
		return opportunityList;
	}
	
	
	public List<Opportunity> findReport(String periodFrom, String periodTo,String statusType, String stageType)
	{

				System.out.println(statusType+"-"+stageType);
				
				StringBuffer buffer = new StringBuffer("SELECT o FROM Opportunity o");
				String cond = " WHERE ";
				if(statusType != null && (!statusType.equalsIgnoreCase("")) && (!statusType.equalsIgnoreCase("0"))){
				buffer.append(cond);
				buffer.append(" o.opportunityStatus.id = :status");
				cond = " AND ";
				}
				if(stageType != null && (!stageType.equalsIgnoreCase(""))&& (!stageType.equalsIgnoreCase("0"))){
				buffer.append(cond);
				buffer.append(" o.stage.id = :stage");
				cond = " AND ";
				}
				
				if( (periodFrom != null && (!periodFrom.equalsIgnoreCase(""))) &&
				(periodTo != null && (!periodTo.equalsIgnoreCase("")))){
				buffer.append(cond);
				buffer.append("o.forecastClosingDate BETWEEN :fromDate AND :toDate");
				cond = " AND ";
				}	
				
				
				
				System.out.println("Created Query Start >> "+buffer.toString());
				
				Query query = em.createQuery(buffer.toString());
				if(statusType != null && (!statusType.equalsIgnoreCase(""))&& (!statusType.equalsIgnoreCase("0")) ){
				query.setParameter("status", new Long(statusType));
				}
				
				if(stageType != null && (!stageType.equalsIgnoreCase("")) && (!stageType.equalsIgnoreCase("0"))){
				query.setParameter("stage", new Long(stageType));
				}
				
				if( (periodFrom != null && (!periodFrom.equalsIgnoreCase(""))) &&
				(periodTo != null && (!periodTo.equalsIgnoreCase("")))){
				query.setParameter("fromDate", DateUtil.stringToDate(periodFrom), TemporalType.DATE);
				query.setParameter("toDate", DateUtil.stringToDate(periodTo), TemporalType.DATE);
				}
				
				List<Opportunity> result = query.getResultList();
				System.out.println("OpportunityList : Report : " +result.size());
				return result;
				}

}
